package com.soap.desensitize.strategy.irreversible;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import com.soap.desensitize.exception.strategy.EncodeException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Sha1Strategy extends AbstractIRreversibleEncry<String> {
    private static Sha1Strategy instance = null;
    private Sha1Strategy(){}

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized Sha1Strategy getInstance(){
        if(instance == null){
            instance = new Sha1Strategy();
        }
        return instance;
    }

    @Override
    public String encode(String data) throws EncodeException {
        //信息摘要器                                算法名称
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new EncodeException("加密错误，加密算法不存在");
        }
        //把字符串转为字节数组
        byte[] b = data.getBytes();
        //使用指定的字节来更新我们的摘要
        md.update(b);
        //获取密文  （完成摘要计算）
        byte[] b2 = md.digest();
        //获取计算的长度
        int len = b2.length;
        //16进制字符串
        String str = "0123456789abcdef";
        //把字符串转为字符串数组
        char[] ch = str.toCharArray();

        //创建一个40位长度的字节数组
        char[] chs = new char[len*2];
        //循环20次
        for(int i=0,k=0;i<len;i++) {
            byte b3 = b2[i];//获取摘要计算后的字节数组中的每个字节
            // >>>:无符号右移
            // &:按位与
            //0xf:0-15的数字
            chs[k++] = ch[b3 >>> 4 & 0xf];
            chs[k++] = ch[b3 & 0xf];
        }

        //字符数组转为字符串
        return new String(chs);
    }
}
